//
//  CUserPageCtrl.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-19.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CUserPageCtrl.h"

@implementation CUserPageCtrl

static CUserPageCtrl    *g_Instance = nil;

//topbar
@synthesize m_TopBar;
@synthesize m_Title;
@synthesize m_LogoutBtn;
@synthesize m_BackBtn;
@synthesize m_OptionBtn;
@synthesize m_LoginBtn;
@synthesize m_RegBtn;
@synthesize m_nIndex;
@synthesize m_LoginView;
@synthesize m_RestisterView;
@synthesize m_sUserIconUrl;
@synthesize m_MyFoodView;
@synthesize m_InfoView;
@synthesize m_OptionView;
@synthesize m_FriendsLst;
@synthesize m_AttentionView;
@synthesize m_AttentionData;
@synthesize m_AttentionDetail;
@synthesize m_ForgetView;

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (!g_Instance)
        {
            g_Instance = [super allocWithZone:zone];
        }
    }
    
    return g_Instance;
}

+(CUserPageCtrl*)DefaultUserPage
{
    if (!g_Instance)
    {
        g_Instance = [[CUserPageCtrl alloc] init];
    }
    
    return g_Instance;
}

+(void)DestroyUserPage
{
    if (g_Instance)
    {
        [g_Instance release];
    }
    
    g_Instance = nil;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bg.image = [UIImage imageNamed:@"appbg.jpg"];
    [self.view addSubview:bg];
    [bg release];
    
    m_TopBar = [[CTopBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, TOPBAR_HEIGHT)];
    m_TopBar.delegate = self;
    m_TopBar.tag = 0;
    [self SetUpTopBar];
    [self.view addSubview:m_TopBar];
    [m_TopBar release];
    
    //loginview
    m_LoginView = [[CLoginView alloc] initWithFrame:CGRectMake(0, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_LoginView.tag = 1;
    m_LoginView.m_Target = self;
    m_LoginView.m_Action = @selector(DidLoginBtnClicked:Value:);
    [self.view addSubview:m_LoginView];
    [m_LoginView release];
    
    //forgetpwd
    m_ForgetView = [[CForgetPwd alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_ForgetView.tag = 9;
    m_ForgetView.m_Target = self;
    m_ForgetView.m_Action = @selector(DidForgerPwdBtnClicked:Value:);
    [self.view addSubview:m_ForgetView];
    [m_ForgetView release];
    
    //registeview
    m_RestisterView = [[CRegisterView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_RestisterView.tag = 2;
    m_RestisterView.m_Target = self;
    m_RestisterView.m_Action = @selector(DidRgBtnClicked:Value:);
    [self.view addSubview:m_RestisterView];
    [m_RestisterView release];
    
    //myfoodview
    m_MyFoodView = [[CInfoView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_MyFoodView.tag = 3;
    m_MyFoodView.delegate = self;
    [self.view addSubview:m_MyFoodView];
    [m_MyFoodView release];
    
    //optionview
    m_OptionView = [[COptionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_OptionView.tag = 4;
    m_OptionView.delegate = self;
    [self.view addSubview:m_OptionView];
    [m_OptionView release];
    
    /*friedns list
    m_FriendsLst = [[CFriendListView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_FriendsLst.tag = 5;
    [self.view addSubview:m_FriendsLst];
    [m_FriendsLst release];
    
    //attention view
    m_AttentionView = [[CAttentionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_AttentionView.tag = 6;
    m_AttentionView.delegate = self;
    [self.view addSubview:m_AttentionView];
    [m_AttentionView release];
    
    //attention data
    m_AttentionData = [[CAttentionDataView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_AttentionData.tag = 7;
    [self.view addSubview:m_AttentionData];
    [m_AttentionData release];
    
    //attention detail
    m_AttentionDetail = [[CAttentionDetail alloc] initWithFrame:CGRectMake(self.view.frame.size.width, m_TopBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-m_TopBar.frame.size.height)];
    m_AttentionDetail.tag = 8;
    [self.view addSubview:m_AttentionDetail];
    [m_AttentionDetail release];*/
    
    [self.view bringSubviewToFront:m_TopBar];
    m_nIndex = 1;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)OnTopBarBackBtnDown:(UIButton*)sender
{
    //option view
    if (4 == m_nIndex)
    {
        [self SetTopBarTitle:nil Title:@"我的美食"];
        m_nIndex = 3;
        [self HiddenOptionView];
    }
    //register view
    else if (2 == m_nIndex)
    {
        [self SetTopBarTitle:nil Title:@"登陆好吃客"];
        m_nIndex = 0;
        [self HiddenRegisterView];
    }
    //friends list
    else if (5 == m_nIndex)
    {
        [self SetTopBarTitle:nil Title:@"我的美食"];
        m_nIndex = 3;
        [self HiddenFriendsList];
    }
    //attention view
    else if (6 == m_nIndex)
    {
        [self SetTopBarTitle:nil Title:@"我的美食"];
        m_nIndex = 3;
        [self HiddenAttentionView];
    }
    //attintion data
    else if (7 == m_nIndex)
    {
        [self SetTopBarTitle:nil Title:@"我的关注"];
        m_nIndex = 6;
        [self HiddenAttentionData];
    }
    //attention detail
    else if (8 == m_nIndex)
    {
        m_nIndex = 7;
        [self HiddenAttentionDetail];
    }
    else if (9 == m_nIndex)
    {
        m_nIndex = 1;
        [self HiddenForgetView];
    }
}

//topbar
-(void)SetUpTopBar
{    
    m_TopBar.layer.shadowOffset = CGSizeMake(0, 1);
    m_TopBar.layer.shadowColor  = [[UIColor blackColor] CGColor];
    m_TopBar.layer.shadowOpacity= 0.8;
    
    UIImageView *m_TopLog = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hchk.png"]];
    m_TopLog.frame = CGRectMake(0, 0, 110, 36);
    m_TopLog.center= CGPointMake(m_TopBar.frame.size.width/2, m_TopBar.frame.size.height/2-2);
    [m_TopBar addSubview:m_TopLog];
    [m_TopLog release];
    
    /*m_LogoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    m_LogoutBtn.frame = CGRectMake(15, 7, 52, 30);
    m_LogoutBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [m_LogoutBtn setTitle:@"  登出" forState:UIControlStateNormal];
    [m_LogoutBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    m_LogoutBtn.tag = 0;
    [m_TopBar AddItem:m_LogoutBtn];
    m_LogoutBtn.hidden = YES;*/
    
    m_OptionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    m_OptionBtn.frame = CGRectMake(m_TopBar.frame.size.width-53, 4, 42, 43);
    [m_OptionBtn setBackgroundImage:[UIImage imageNamed:@"option.png"] forState:UIControlStateNormal];
    m_OptionBtn.tag = 1;
    [m_TopBar AddItem:m_OptionBtn];
    m_OptionBtn.hidden = YES;
    m_OptionBtn.alpha  = 0.0;
    
    m_BackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBtn.frame = CGRectMake(10, 6, 43, 38);
    [m_BackBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    m_BackBtn.tag = 2;
    [m_TopBar AddItem:m_BackBtn];
    m_BackBtn.hidden = YES;
    
    m_LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    m_LoginBtn.frame = CGRectMake(m_TopBar.frame.size.width-80, 6, 70, 38);
    [m_LoginBtn setBackgroundImage:[UIImage imageNamed:@"loginbtn.png"] forState:UIControlStateNormal];
    m_LoginBtn.tag = 3;
    [m_TopBar AddItem:m_LoginBtn];
    m_LoginBtn.hidden = YES;
    
    m_RegBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    m_RegBtn.frame = CGRectMake(m_TopBar.frame.size.width-80, 6, 70, 38);
    [m_RegBtn setBackgroundImage:[UIImage imageNamed:@"regbtn.png"] forState:UIControlStateNormal];
    m_RegBtn.tag = 4;
    [m_TopBar AddItem:m_RegBtn];
    m_RegBtn.hidden = YES;
    
    m_Title = [[UILabel alloc] initWithFrame:CGRectMake((m_TopBar.frame.size.width-200)/2, 0, 200, TOPBAR_HEIGHT)];
    m_Title.font = [UIFont boldSystemFontOfSize:21];
    m_Title.backgroundColor = [UIColor clearColor];
    m_Title.textColor = [UIColor blackColor];
    m_Title.textAlignment = UITextAlignmentCenter;
    [m_TopBar addSubview:m_Title];
    [m_Title release];
}

-(void)DidTopBarBtnsClicked:(UIButton*)btn
{
    [self OnTopBarDown:btn];
}

-(void)SetTopBarTitle:(NSString*)image Title:(NSString*)title
{
    //m_Title.image = [UIImage imageNamed:image];
    m_Title.text = nil;
}

-(void)OnTopBarDown:(UIButton*)sender
{
    //logout
    if (0 == sender.tag)
    {
        [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"登出中..."];
        [self CommitLogoutRequest];
    }
    //option
    else if (1 == sender.tag)
    {
        [self ShowOptionView];
    }
    //back
    else if (2 == sender.tag)
    {
        [self OnTopBarBackBtnDown:sender];
    }
    //login
    else if (3 == sender.tag)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1;
        [m_LoginView OnButtonDown:btn];
    }
    //register
    else if (4 == sender.tag)
    {
        [m_RestisterView DidCommitBntDown:[UIButton buttonWithType:UIButtonTypeCustom]];
    }
    else {;}
}

//self
-(void)ShowOptionPageCtrl
{
    if (self.view.hidden)
    {
        [self DidOptionPageCtrlShown];
        self.view.hidden = NO;
    }
}

-(void)DidOptionPageCtrlShown
{
    if (Logined != [CUser DefaultUser].m_UType)
    {
        [self ShowLoginView];
    }
    else 
    {
        if (m_MyFoodView.m_UserID != [[CUser DefaultUser].U_AID longValue])
        {
            [self ShowMyInfoView:YES];
        }
        else 
        {
            [m_MyFoodView SetInfoViewInfo];
        }
    }
}

//my info
-(void)ShowMyInfoView:(BOOL)banimation
{
    if (banimation)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(DidMyInfoViewShown)];
        m_nIndex           = m_MyFoodView.tag;
        m_LogoutBtn.hidden = NO;
        m_OptionBtn.hidden = NO;
        [self SetTopBarTitle:nil Title:@"我的美食"];
        m_MyFoodView.center = CGPointMake(self.view.frame.size.width/2, m_MyFoodView.center.y);
        [UIView commitAnimations];
    }
    else 
    {
        m_nIndex           = m_MyFoodView.tag;
        m_LogoutBtn.hidden = NO;
        m_OptionBtn.hidden = NO;
        [self SetTopBarTitle:nil Title:@"我的美食"];
        m_MyFoodView.center = CGPointMake(self.view.frame.size.width/2, m_MyFoodView.center.y);
        [self DidMyInfoViewShown];
    }
}

-(void)DidInfoViewHidden
{
    [m_MyFoodView ClearData];
}

-(void)HiddenInfoView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidInfoViewHidden)];
    m_OptionBtn.hidden = YES;
    m_MyFoodView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_MyFoodView.center.y);
    [UIView commitAnimations];
}

-(void)DidMyInfoViewShown
{
    [m_MyFoodView SetInfoViewInfo];
}

-(void)DidClickButtonIndexInfoView:(UIButton *)button Object:(id)object
{
    //attention
    if (0 == button.tag)
    {
        [self ShowAttentionView];
    }
    //friends
    else if (1 == button.tag)
    {
        [self ShowFriendsList];
    }
    //lougout
    else if (2 == button.tag)
    {
        [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"登出中..."];
        [self CommitLogoutRequest];
    }
    else {;}
}

//option view
-(void)ShowOptionView
{
    [m_OptionView SetOptionViewInfo];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_nIndex = m_OptionView.tag;
    m_LogoutBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_OptionBtn.hidden = YES;
    [self SetTopBarTitle:nil Title:@"设置"];
    m_OptionView.center = CGPointMake(self.view.frame.size.width/2, m_OptionView.center.y);
    [UIView commitAnimations];
}

-(void)HiddenOptionView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_LogoutBtn.hidden = NO;
    m_BackBtn.hidden   = YES;
    m_OptionBtn.hidden = NO;
    m_OptionView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_OptionView.center.y);
    [UIView commitAnimations];
}

//friends list
-(void)ShowFriendsList
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidFriendsListShown)];
    m_nIndex = m_FriendsLst.tag;
    m_LogoutBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_OptionBtn.hidden = YES;
    [self SetTopBarTitle:nil Title:@"我的好友"];
    m_FriendsLst.center = CGPointMake(self.view.frame.size.width/2, m_FriendsLst.center.y);
    [UIView commitAnimations];
}

-(void)DidFriendsListShown
{
    [m_FriendsLst SetFrdLstViewInfo];
}

-(void)HiddenFriendsList
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidFriendsListHidden)];
    m_LogoutBtn.hidden = NO;
    m_BackBtn.hidden   = YES;
    m_OptionBtn.hidden = NO;
    m_FriendsLst.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_FriendsLst.center.y);
    [UIView commitAnimations];
}

-(void)DidFriendsListHidden
{
    [m_FriendsLst ClearData];
}

//attention view
-(void)ShowAttentionView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_nIndex = m_AttentionView.tag;
    m_OptionBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_LogoutBtn.hidden = YES;
    [self SetTopBarTitle:nil Title:@"我的关注"];
    m_AttentionView.center = CGPointMake(self.view.frame.size.width/2, m_AttentionView.center.y);
    [UIView commitAnimations];
}

-(void)DidAttentionShown
{
    //
}

-(void)HiddenAttentionView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_OptionBtn.hidden = NO;
    m_BackBtn.hidden   = YES;
    m_LogoutBtn.hidden = NO;
    m_AttentionView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_AttentionView.center.y);
    [UIView commitAnimations];
}

-(void)DidAttentionHidden
{
    //
}

-(void)DidAttentionViewSelectedRow:(int)row Object:(id)object
{
    //people
    if (0 == row)
    {
        m_AttentionData.m_nOpType = row;
        [self ShowAttentionData:@"我关注的人"];
    }
    //food
    else if (1 == row)
    {
        m_AttentionData.m_nOpType = row;
        [self ShowAttentionData:@"我关注的菜"];
    }
    //place
    else if (2 == row)
    {
        m_AttentionData.m_nOpType = row;
        [self ShowAttentionData:@"我关注的地方"];
    }
    else {;}
}

//attention data
-(void)ShowAttentionData:(NSString*)title
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidAttentionDataShown)];
    m_nIndex = m_AttentionData.tag;
    m_OptionBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_LogoutBtn.hidden = YES;
    [self SetTopBarTitle:nil Title:title];
    m_AttentionData.center = CGPointMake(self.view.frame.size.width/2, m_AttentionData.center.y);
    [UIView commitAnimations];
}

-(void)DidAttentionDataShown
{
    [m_AttentionData SetAttentionDatas:m_AttentionData.m_nOpType];
}

-(void)HiddenAttentionData
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidAttentionDataHidden)];
    m_OptionBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_LogoutBtn.hidden = YES;
    m_AttentionData.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_AttentionData.center.y);
    [m_AttentionData.m_SearchBar resignFirstResponder];
    [UIView commitAnimations];
}

-(void)DidAttentionDataHidden
{
    [m_AttentionData ClearDatas];
}

//attention detail
-(void)ShowAttentionDetail:(NSString*)title
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidAttentionDetailShown)];
    m_nIndex = m_AttentionDetail.tag;
    m_OptionBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_LogoutBtn.hidden = YES;
    m_AttentionDetail.center = CGPointMake(self.view.frame.size.width/2, m_AttentionDetail.center.y);
    [UIView commitAnimations];
}

-(void)DidAttentionDetailShown
{
    [m_AttentionDetail.m_TableView reloadData];
}

-(void)HiddenAttentionDetail
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidAttentionDetailHidden)];
    m_OptionBtn.hidden = YES;
    m_BackBtn.hidden   = NO;
    m_LogoutBtn.hidden = YES;
    m_AttentionDetail.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_AttentionData.center.y);
    [m_AttentionDetail.m_SearchBar resignFirstResponder];
    [UIView commitAnimations];
}

-(void)DidAttentionDetailHidden
{
    [m_AttentionDetail ClearDatas];
}

-(void)DidOptionTableViewSelected:(int)index
{
    //my infomation 0 1
    if (0 == index)
    {
        [self HiddenOptionView];
    }
    //my friends 1 0
    else if (1 == index)
    {
        //[self ShowFriendsList];
        [self HiddenOptionView];
    }
    //acount
    else if (2 == index)
    {
        [self HiddenOptionView];
    }
    else {;}
}

-(void)HiddenOptionPageCtrl
{
    if (!self.view.alpha)
    {
        self.view.hidden = YES;
    }
}

-(void)UserPageHidden
{
    //my food
    if (3 == [CUserPageCtrl DefaultUserPage].m_nIndex)
    {
        [m_MyFoodView ClearData];
        m_MyFoodView.m_UserID = [[CUser DefaultUser].U_AID longValue];
    }
}

-(void)DidReturnDown
{
    //[m_InfoView DidReturnDown];
}

-(void)SetUserPageInfo
{
    [m_InfoView SetInfoViewInfo];
}

//注册成功
-(void)ShowRegisterView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_nIndex           = m_RestisterView.tag;
    m_LogoutBtn.hidden = YES;
    m_OptionBtn.hidden = YES;
    m_LoginBtn.hidden  = YES;
    m_BackBtn.hidden   = NO;
    m_RegBtn.hidden    = NO;
    
    m_RestisterView.m_Pwd.text    = nil;
    m_RestisterView.m_Nick.text   = nil;
    m_RestisterView.m_Mail.text   = nil;
    m_RestisterView.m_Icon.image  = nil;
    
    [self SetTopBarTitle:nil Title:@"注册"];
    m_RestisterView.m_Icon.image = [UIImage imageNamed:@"dicon.png"];
    m_RestisterView.center = CGPointMake(self.view.frame.size.width/2, m_RestisterView.center.y);
    [UIView commitAnimations];
}

-(void)HiddenRegisterView
{
    [m_RestisterView HiddenKeyBoard];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_LogoutBtn.hidden = YES;
    m_OptionBtn.hidden = YES;
    m_BackBtn.hidden   = YES;
    m_LoginBtn.hidden  = NO;
    m_RegBtn.hidden    = YES;
    m_RestisterView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_RestisterView.center.y);
    [UIView commitAnimations];
}

-(void)DidRgBtnClicked:(NSNumber*)number Value:(id)value
{
    int nRet = [[CUser DefaultUser] CheckUserRegInput:m_RestisterView.m_Nick.text Mail:m_RestisterView.m_Mail.text PasWd:m_RestisterView.m_Pwd.text];
    
    NSString *meg = @"注册错误.";
    if (257 == nRet)
    {
        //user's name is nil
        meg = @"昵称不能为空.";
    }
    else if (258 == nRet)
    {
        //user's pwd is nil
        meg = @"密码长度不少于6位.";
    }
    else if (256 == nRet)
    {
        meg = @"邮箱格式不正确.";
    }
    else {;}
    
    if (0 != nRet)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:meg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [alter show];
        [alter autorelease];
    }
    else 
    {    
        [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"注册中..."];
        //上传个人头像
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestError:) name:SRV_REQUEST_ERROR object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestTimeOut:) name:SRV_REQUEST_TIMEOUT object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UploadIconReponse:) name:SRV_USERICON object:nil];
        
        [[CUser DefaultUser] UserUploadIcon:UIImageJPEGRepresentation(m_RestisterView.m_Icon.image, 0.5)];
    }
}

-(void)UploadIconReponse:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_USERICON object:nil];
    
    NSDictionary *dic = notification.object;
    if (dic)
    {
        //NSLog(@"%@", dic);
        self.m_sUserIconUrl = [dic objectForKey:@"big"];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestError:) name:SRV_REQUEST_ERROR object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestTimeOut:) name:SRV_REQUEST_TIMEOUT object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RegisterReponse:) name:SRV_REGISTER object:nil];
        
        int nRet = [[CUser DefaultUser] UserRegister:m_RestisterView.m_Nick.text PassWord:m_RestisterView.m_Pwd.text EMail:m_RestisterView.m_Mail.text City:m_RestisterView.m_Citytf.text Nick:m_RestisterView.m_Nick.text];
        if (0 != nRet) 
        {    
            [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
            
            [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REGISTER object:nil];
        }
    }
}

-(void)RegisterReponse:(NSNotification*)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dic = notification.object;
        [self performSelectorOnMainThread:@selector(DidRegisterRet:) withObject:dic waitUntilDone:YES];
    }
}

-(void)DidRegisterRet:(NSDictionary*)dic
{
    int nCode = [[dic objectForKey:@"Msg_error"] intValue];
    NSString *msg = nil;
    
    if (0 == nCode)
    {
        CUser *user = [CUser DefaultUser];
        //头像url写到数据库中
        NSMutableArray *keys = [NSMutableArray array];
        CKey *key = [[CKey alloc] initWithType:3 Value:self.m_sUserIconUrl Name:@"U_UrlIcon"];
        [keys addObject:key];
        [key release];
        [CUser UpadteUserInfo:[NSString stringWithFormat:@"U_AID==%lld", [user.U_AID longLongValue]] Keys:keys];
        //头像保存到本地
        [user SaveIcon:UIImageJPEGRepresentation(m_RestisterView.m_Icon.image, 1)];
        
        m_LoginView.m_LoginName.text = user.U_Name;
        m_LoginView.m_LoginPwd.text  = user.U_Pas;
        
        m_RegBtn.hidden = YES;
        m_LoginBtn.hidden=YES;
        m_OptionBtn.hidden=NO;
        
        m_RestisterView.m_Pwd.text    = nil;
        //m_RestisterView.m_Name.text   = nil;
        m_RestisterView.m_Nick.text   = nil;
        m_RestisterView.m_Mail.text   = nil;
        //m_RestisterView.m_Citytf.text = nil;
        m_RestisterView.m_Icon.image  = nil;
        
        msg = @"注册成功";
        
        [self DidRegisterSuccessed];
    }
    else 
    {
        msg = [dic objectForKey:@"Msg"];
    }
    
    UIAlertView *suc = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
    [suc show];
    [suc autorelease];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REGISTER object:nil];
    
    self.m_sUserIconUrl = nil;
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
}

-(void)DidRegisterSuccessed
{
    [self ShowMyInfoView:NO];
    [self HiddenRegisterView];
    m_LogoutBtn.hidden = NO;
    m_LoginBtn.hidden  = YES;
    m_OptionBtn.hidden = NO;
}

//login
-(void)CommitLoginRequest
{
    int nRet = 0;
    if ((nRet = [[CUser DefaultUser] UserLogin:m_LoginView.m_LoginName.text PassWord:[m_LoginView.m_LoginPwd.text lowercaseString] EMail:nil City:nil]))
    {
        NSString *msg = nil;
        if (257 == nRet)
        {
            msg = @"登陆名必须是1-10个字符长";
        }
        else if (256 == nRet)
        {
            msg = @"邮箱地址输入不正确";
        }
        else if (258 == nRet)
        {
            msg = @"密码必须是6-18个字符长";
        }
        else 
        {
            msg = @"输入有误";
        }
        
        UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [error show];
        [error autorelease];
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_LOGIN object:nil];
        
        [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
    }
    else 
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestError:) name:SRV_REQUEST_ERROR object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestTimeOut:) name:SRV_REQUEST_TIMEOUT object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoginResponse:) name:SRV_LOGIN object:nil];
    }
}

-(void)LoginResponse:(NSNotification*)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dic = notification.object;
        int nCode = [[dic objectForKey:@"Msg_error"] intValue];
        NSString *msg = nil;
        
        if (0 == nCode)
        {
            m_LogoutBtn.hidden = NO;
            m_LoginBtn.hidden = YES;
            m_OptionBtn.hidden= NO;
            
            //m_LoginView.m_LoginBtn.hidden     = YES;
            //m_LoginView.m_LogoutBtn.hidden    = NO;
            //m_LoginView.m_FindPwdBtn.hidden   = YES;
            //m_LoginView.m_RegisterBtn.enabled = YES;
            //m_LoginView.m_LoginName.enabled   = NO;
            //m_LoginView.m_LoginPwd.enabled    = NO;
            
            //登陆成功，进入我的美食
            [self ShowMyInfoView:YES];
            
            [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
            
            //msg = @"登陆成功";
        }
        else 
        {
            [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
            //msg = [dic objectForKey:@"Msg"];
            msg = @"用户名不存在或密码错误";
            
            UIAlertView *suc = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [suc show];
            [suc autorelease];
        }
    }
    else
    {
        [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
    }
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_LOGIN object:nil];
}

-(void)DidLoginBtnClicked:(NSNumber*)number Value:(id)value
{
    int index = [number intValue];
    
    //login
    if (1 == index)
    {
        [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"登录中..."];
        [self CommitLoginRequest];
    }
    //logout
    else if (3 == index)
    {
        [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"登出中..."];
        [self CommitLogoutRequest];
    }
    //register
    else if (2 == index)
    {    
        [self ShowRegisterView];
    }
    //find pwd
    else if (0 == index)
    {
        [self ShowForgetView];
    }
    else{;}
}

-(void)ShowLoginView
{
    [UIView beginAnimations:nil context:nil];
    m_LogoutBtn.hidden = YES;
    m_LoginBtn.hidden  = NO;
    m_RegBtn.hidden    = YES;
    [self SetTopBarTitle:nil Title:@"登陆好吃客"];
    m_LoginView.center = CGPointMake(self.view.frame.size.width/2, m_LoginView.center.y);
    [UIView commitAnimations];
}

-(void)HiddenLoginView
{
    [UIView beginAnimations:nil context:nil];
    m_LoginView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_LoginView.center.y);
    [UIView commitAnimations];
}

//forgetpwd
-(void)ShowForgetView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_nIndex = m_ForgetView.tag;
    m_BackBtn.hidden = NO;
    m_LoginBtn.hidden= YES;
    m_RegBtn.hidden  = YES;
    m_ForgetView.center = CGPointMake(self.view.frame.size.width/2, m_ForgetView.center.y);
    [UIView commitAnimations];
}

-(void)HiddenForgetView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    m_BackBtn.hidden = YES;
    m_LoginBtn.hidden= NO;
    m_RegBtn.hidden  = YES;
    m_ForgetView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_ForgetView.center.y);
    [UIView commitAnimations];
}

-(void)DidForgerPwdBtnClicked:(NSNumber*)number Value:(id)value
{
    [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"处理中..."];
    [self CommitFindPwdRequest:value];
}

//logout
-(void)CommitLogoutRequest
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestError:) name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestTimeOut:) name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LogoutResponse:) name:SRV_LOGOUT object:nil];
    
    [[CUser DefaultUser] UserLogout];
}

-(void)LogoutResponse:(NSNotification*)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]])
    {
        if (1)
        {
            [CUserPageCtrl DefaultUserPage].m_LogoutBtn.hidden = YES;
            
            m_LoginBtn.hidden = NO;
            m_OptionBtn.hidden= YES;
            m_RegBtn.hidden   = YES;
            
            //m_LoginView.m_LoginBtn.hidden     = NO;
            //m_LoginView.m_LogoutBtn.hidden    = YES;
            //m_LoginView.m_FindPwdBtn.hidden   = NO;
            //m_LoginView.m_RegisterBtn.hidden  = NO;
            //m_LoginView.m_LoginName.enabled   = YES;
            //m_LoginView.m_LoginPwd.enabled    = YES;
            
            m_LoginView.m_LoginName.text = nil;
            m_LoginView.m_LoginPwd.text  = nil;
            
            //登陆成功，回到登陆页面
            m_nIndex = 1;
            [self HiddenInfoView];
            
            //清除草稿
            [[CDBManager ScheduledShareInstance] ClearTable:@"Draft"];
            [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"Draft"] error:nil];
            [[AppConfig DefaultAppConfig] CreatePathInApp:@"Documents" SubPath:@"Draft"];

            [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
            
            //UIAlertView *suc = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登出成功" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            //[suc show];
            //[suc autorelease];
        }
    }
    else 
    {
        [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
    }
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_LOGOUT object:nil];
}

//find password
-(void)CommitFindPwdRequest:(NSString*)email
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestError:) name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RequestTimeOut:) name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FindPwdResponse:) name:SRV_FINDPWD object:nil];
    
    if (1 == [[CUser DefaultUser] UserFindPwd:email])
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_FINDPWD object:nil];
        
        [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
        
        UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的邮箱地址" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [error show];
        [error autorelease];
    }
}

-(void)FindPwdResponse:(NSNotification*)notification
{
    //NSLog(@"\r\n\r\n找回密码结果:%@\r\n\r\n", notification.object);
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_FINDPWD object:nil];
    
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
    
    NSDictionary *dic = notification.object;
    if (0 == [[dic objectForKey:@"Msg_error"] intValue])
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:m_LoginView.m_BackView cache:YES];
        [UIView setAnimationDuration:1];
        m_LoginView.m_Email.text = nil;
        [m_LoginView.m_BackView exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
        [UIView commitAnimations];
    }
    
    UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"提示" message:[dic objectForKey:@"Msg"] delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
    [error show];
    [error autorelease];
}

//request
-(void)RequestError:(NSNotification *)mes
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_LOGIN object:nil];
    
    [self performSelectorOnMainThread:@selector(UpdateUIWhenRequestError:) withObject:mes waitUntilDone:NO];
}

-(void)UpdateUIWhenRequestError:(id)object
{
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
}

-(void)RequestTimeOut:(NSNotification *)mes
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_ERROR object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_REQUEST_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SRV_LOGIN object:nil];
    
    [self performSelectorOnMainThread:@selector(UpdateUIWhenRequestTimeout:) withObject:mes waitUntilDone:NO];
}

-(void)UpdateUIWhenRequestTimeout:(id)object
{
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
}

@end
